package com.franklin.validation.demo;

import com.franklin.validation.core.annotations.NotNullForMap;
import com.franklin.validation.core.annotations.ParamCheck;
import com.franklin.validation.core.annotations.ParamCheckSub;
import com.franklin.validation.core.utils.DataCheckUtil;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/3 19:36
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/1")
    @NotNullForMap(params = {
            @ParamCheck(value = "personList",type = List.class,subParams = {
                    @ParamCheckSub("id"),
                    @ParamCheckSub("name"),
                    @ParamCheckSub(value = "birth",type = Date.class,pattern = "yyyy-MM-dd",message = "生日错误"),
            }),
            @ParamCheck(value = "person",type = Map.class,subParams = {
                    @ParamCheckSub("id"),
                    @ParamCheckSub("name"),
                    @ParamCheckSub(value = "birth",type = Date.class,pattern = "yyyy-MM-dd",message = "生日错误"),
            })
    })
    public String test1(@RequestBody Map<String,Object> param){
        return param.toString();
    }

    @PostMapping("/2")
    @NotNullForMap(version = "2",params = {
            @ParamCheck(required = false,max = 3,defaultValue = "432423",value = "personList",pattern = "^[1-9]\\d*$",message = "成员列表有误")
    })
    public Map test2(@RequestBody Map<String,Object> param){
        System.out.println(Number.class.isAssignableFrom(Integer.class));
        return param;
    }

    @PostMapping("/3")
    public Map<String,Object> test3(
            @ParamCheck(value = "birth",required = false,type = Date.class,pattern = "yyyy-MM-dd")String birth,
            @ParamCheck(value = "name",required = false)String name,
            @ParamCheck(value = "age",required = false)String age,
            @ParamCheck(value = "password",required = false)String password,
            @ParamCheck(value = "sign",required = false)String sign,
            @ParamCheck("id")List<String> id,
            @ParamCheck("id1")List<String> id1,
            @ParamCheck("id2")List<String> id2,
            @ParamCheck("id3")List<String> id3,
            @ParamCheck("id4")List<String> id4,
            @ParamCheck("id5")List<String> id5,
            @ParamCheck("id6")List<String> id6,
            @ParamCheck("id7")Integer id7
    ){
            Map<String,Object> map = new HashMap<>();
            map.put("birth",birth);
            map.put("name",name);
            map.put("age",age);
            map.put("password",password);
            map.put("sign",sign);
            map.put("id",id);
            map.put("id1",id1);
            map.put("id2",id2);
            map.put("id3",id3);
            map.put("id4",id4);
            map.put("id5",id5);
            map.put("id6",id6);
            map.put("id7",id7);
        return map;
    }

    @PostMapping(value = "/4",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Map<String,Object> test4(
            @RequestPart("file")FilePart filePart,
            @RequestPart("id")String id,
            @RequestPart("test")String test,
            @RequestHeader("requestId")String re
    ){
        Path tempFile = null;
        try {
            tempFile = Files.createTempFile("excels", filePart.filename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = tempFile.toFile();
        filePart.transferTo(file);
        System.out.println("~~~~~~~~~~~~~UploadFilePath : " + file.getPath());
        Map<String,Object> map = new HashMap<>();
        map.put("file",file);
        map.put("id",id);
        map.put("test",test);
        return map;
    }

    @PostMapping("/5")
    public String test5(@ParamCheck(value = "id",required = true,pattern = "yyyy-MM-dd",type = Date.class)String id){
        return id;
    }

    @PostMapping("/6")
    public Collection<Integer> test5(@ParamCheck(value = "id",pattern = "^[1-9]\\d*$")Set<Integer> id){
        return id;
    }
}
